package com.example.android.accelerometerplay;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SpeakPhoneTestActivity extends Activity{
	/**
	 * Set the speak phone state 
	 * Need MODIFY_AUDIO_SETTINGS
	 * 
	 * 
	 * */
	
	private TextView m_pChild_Txt_SpeakPhoneState = null;
	private Button m_pChild_Btn_SpeakPhoneON = null;
	private Button m_pChild_Btn_SpeakPhoneOFF = null;
	
	private AudioManager myAM = null;
	
	private boolean bCurrentSpeakPhoneState = false;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(R.string.STR_0004);
        setContentView(R.layout.speakphone_test);
        // Initialize buttons
        InitBtns();
        Tele();
        
        //
        BluetoothReceiver mBtReceiver = new BluetoothReceiver();
        IntentFilter ittFilter= new IntentFilter(Intent.ACTION_CALL);
        ittFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        ittFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        registerReceiver(mBtReceiver, ittFilter);
    }
	
	private void InitBtns() {
		if (null == m_pChild_Txt_SpeakPhoneState) {
			m_pChild_Txt_SpeakPhoneState = (TextView) findViewById(R.id.Txt_SpeakPhone_State);
		}

		if (null == m_pChild_Btn_SpeakPhoneON) {
			m_pChild_Btn_SpeakPhoneON = (Button) findViewById(R.id.Btn_SpeakPhone_ON);
			m_pChild_Btn_SpeakPhoneON.setOnClickListener(new _Btn_SpeakPhoneON_OnClickListener());
		}

		if (null == m_pChild_Btn_SpeakPhoneOFF) {
			m_pChild_Btn_SpeakPhoneOFF = (Button) findViewById(R.id.Btn_SpeakPhone_OFF);
			m_pChild_Btn_SpeakPhoneOFF.setOnClickListener(new _Btn_SpeakPhoneOFF_OnClickListener());
		}
	}
	
	private void SetSpeakPhoneState(boolean bSpeakerState) {

		if (null == myAM) {
			myAM = (AudioManager) getSystemService(AUDIO_SERVICE);
			myAM.setWiredHeadsetOn(true);
		}

		try {
			myAM.setSpeakerphoneOn(bSpeakerState);
			bCurrentSpeakPhoneState = bSpeakerState;
			SetState();
			return;
		} catch (Exception localException) {
		}
	}

	private void SetState() {
		if (bCurrentSpeakPhoneState) {
			m_pChild_Txt_SpeakPhoneState.setText("ON");
		} else {
			m_pChild_Txt_SpeakPhoneState.setText("OFF");
		}
	}
	
	private boolean IsBtUsing() {
		boolean bRst = false;
		BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		
		if (mBluetoothAdapter == null) {
			// Device does not support Bluetooth
			return bRst;
		}

		if (!mBluetoothAdapter.isEnabled()) {
			// 
			return bRst;
		}
				
		return bRst;
	}

	
	
	private void Tele(){
		TelephonyManager mTelephonyMager = (TelephonyManager) this
				.getSystemService(Context.TELEPHONY_SERVICE);
		
		mTelephonyMager.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
	}
	
	/**
	 * 
	 * Class
	 * 
	 * */
	class TeleListener extends PhoneStateListener {
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			super.onCallStateChanged(state, incomingNumber);

			switch (state) {
			case TelephonyManager.CALL_STATE_IDLE:
				// Normal state
				m_pChild_Txt_SpeakPhoneState.setText("IDLE");
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK:
				m_pChild_Txt_SpeakPhoneState.setText("OFFHOOK");
				break;
			case TelephonyManager.CALL_STATE_RINGING:
				m_pChild_Txt_SpeakPhoneState.setText("RINGING");
				break;
			default:
				break;
			}
		}
	}
	
	public class BluetoothReceiver extends BroadcastReceiver{
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(
					BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
				m_pChild_Txt_SpeakPhoneState.setText("ACTION_ACL_DISCONNECTED");
			} else if (intent.getAction().equals(
					BluetoothDevice.ACTION_ACL_CONNECTED)) {
				m_pChild_Txt_SpeakPhoneState.setText("ACTION_ACL_CONNECTED");
			} else {
				abortBroadcast();
				if (Intent.ACTION_CALL == intent.getAction()) {
					m_pChild_Txt_SpeakPhoneState.setText("ACTION_CALL");
				} else {

				}
			}
		}
	}
	
	/***
     * 
     * Action listener
     * 
     * */
	
    /*-----On Click Listener--------*/
    class _Btn_SpeakPhoneON_OnClickListener implements OnClickListener{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			SetSpeakPhoneState(true);
		}
    }
    
    /*-----On Click Listener--------*/
    class _Btn_SpeakPhoneOFF_OnClickListener implements OnClickListener{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			SetSpeakPhoneState(false);
		}
    }
}
